/**
 * Created by zlj on 2016/12/6.
 */
require('jquery');
require('layout/util');
var $form = require('js-path/formValide.js');
var $form2 = require('js-path/formValideNotSub.js');
var $alert = require('libs/alert');
$(function () {
    $('#channel_back').on('click',function () {
        window.history.go(-1);
    });
    judge();
    formValideinit();
    $('#submit_form').on('click',function () {
        console.info($("#logistics_form input[name=hasAir]").is(':checked')+"--------------------");
        console.info($("#logistics_form input[name=hasOcean]").is(':checked')+"--------------------");
        console.info($("#logistics_form input[name=hasRailway]").is(':checked')+"--------------------");
        console.info($("#logistics_form input[name=hasExpress]").is(':checked')+"--------------------");
        if((!$("#logistics_form input[name=hasFeeAir]").is(':checked'))&&
            (!$("#logistics_form input[name=hasOcean]").is(':checked'))&&
            (!$("#logistics_form input[name=hasRailway]").is(':checked'))&&
            (!$("#logistics_form input[name=hasExpress]").is(':checked'))){
            $alert("请至少勾选一个服务种类");
            return;
        }


        $('#logistics_form').submit();
    })
    //选择服务种类
    $('.tran-box-check span').click(function () {
        var myclass = $(this).attr('name')
        if($(this).hasClass("list-show")){
            $(this).find('input').attr("checked",false);
            $(this).removeClass('list-show')
            $("."+myclass).hide();
        }else{
            $(this).find('input').attr("checked",true);
            $(this).addClass('list-show');
            $("."+myclass).show();
        }
        valideServiceType($(this).find('input'))
    })

    $("#logistics_form input[name=biaozhunOcean]").each(function(){
        $(this).on("click",function(){
            var discount = $(this).val();
            if(discount=="minFeeWeightOcean" && $('input[name=hasOcean]').is(':checked')){
                $("#logistics_form input[name=minFeeWeightOcean]")
                    .rules("add",{required:true,number:true});

            }else{
                $("#logistics_form input[name=minFeeWeightOcean]")
                    .rules("remove","required");
            }
            if(discount=="minFeeVolumnOcean"&& $('input[name=hasOcean]').is(':checked')){
                $("#logistics_form input[name=minFeeVolumnOcean]")
                    .rules("add",{required:true,number:true});
            }else{
                $("#logistics_form input[name=minFeeVolumnOcean]")
                    .rules("remove","required");
            }
        });
    });

    $("#logistics_form select[name=hasVolumnFeeAir]").on("change",function(){
        if($(this).val()=='Y'){
            $("#logistics_form input[name=volumnParamAir]")
                .rules("add",{required:true});
            $("#hasVolumnFeeAirLabel").html("<i>*</i>体积参数：");
        }else{
            $("#logistics_form input[name=volumnParamAir]")
                .rules("remove","required");
            $("#hasVolumnFeeAirLabel").html("体积参数：");
        }
    });
    $("#logistics_form select[name=hasVolumnFeeExpress]").on("change",function(){
        if($(this).val()=='Y'){
            $("#logistics_form input[name=volumnParamExpress]")
                .rules("add",{required:true});
            $("#hasVolumnFeeExpressLabel").html("<i>*</i>体积参数：");
        }else{
            $("#logistics_form input[name=volumnParamExpress]")
                .rules("remove","required");
            $("#hasVolumnFeeExpressLabel").html("体积参数：");
        }
    });

});
//表单验证
function formValideinit() {
    $form({selector:"#logistics_form"},{
        code:{required:true,hasSpecial:true,remote:{url:'api/v1/p/mms/internationalLogisticsProvider/isExsit', type: "post",
            data: { code : function(){ return $("#logistics_form input[name=code]").val()}, id : function(){ return $("#logistics_form input[name=id]").val()}},dataFilter:function (result) {
                var  obj = JSON.parse(result);
                if(obj.code == '200' ){
                    if(!obj.data){return true} else { return false}
                }else{
                    return false;
                }
            }
        }},
        name:{required:true},
        linkman:{required:true},
        linkTelphone:{required:true},
        address:{required:true}
    },{code:{required:'国际物流渠道简称必填',remote:'国际物流渠道简称已存在'},
        name:{required:'国际物流渠道名称必填'},
            linkman:{required:'联系人必填'},
        linkTelphone:{required:'联系电话必填'},
            address:{required:'联系地址必填'}
    },asyfun,function (result) {
        if(result.code == 200){
            window.location.href = 'Logistics-provider-list';
        }else{
            $alert(result.message);
        }
    })
};
function valideServiceType(obj) {
    if(obj.attr('name') =='hasOcean'){
        var discount=$("input[name=biaozhunOcean]:checked").val();
        if ($('input[name=hasOcean]').is(':checked')) {
            $("#logistics_form input[name=minDiscountOcean]")
                .rules("add",{required:true})
            if(discount=="minFeeWeightOcean" ){
                $("#logistics_form input[name=minFeeWeightOcean]")
                    .rules("add",{required:true,number:true});

            }
            if(discount=="minFeeVolumnOcean"){
                $("#logistics_form input[name=minFeeVolumnOcean]")
                    .rules("add",{required:true,number:true});
            }

        }else{
            $("#logistics_form input[name=minDiscountOcean]")
                .rules("remove","required");

            $("#logistics_form input[name=minFeeWeightOcean]")
                .rules("remove","required");
            $("#logistics_form input[name=minFeeVolumnOcean]")
                .rules("remove","required");
        }
    }
    if(obj.attr('name') =='hasAir'){
        if ($('input[name=hasAir]').is(':checked')) {
            $("#logistics_form input[name=minFeeWeightAir]")
                .rules("add",{number:true});
            $("#logistics_form input[name=minDiscountAir]")
                .rules("add",{number:true});
            $("#logistics_form input[name=maxBoxWeightAir]")
                .rules("add",{required:true});
            $("#logistics_form input[name=maxBoxLengthAir]")
                .rules("add",{required:true});
            $("#logistics_form input[name=maxInsureAmountAir]")
                .rules("add",{number:true});
            if($("#logistics_form select[name=hasVolumnFeeAir]").val()=='Y'){
                $("#logistics_form input[name=volumnParamAir]")
                    .rules("add",{required:true});
                $("#hasVolumnFeeAirLabel").html("<i>*</i>体积参数：");

            }else{
                $("#logistics_form input[name=volumnParamAir]")
                    .rules("remove","required");
                $("#hasVolumnFeeAirLabel").html("体积参数：");
            }

        }else{
            $("#logistics_form input[name=volumnParamAir]")
                .rules("remove","required");
            $("#logistics_form input[name=maxBoxWeightAir]")
                .rules("remove","required");
        }
    }
    if(obj.attr('name') =='hasExpress'){
        if ($('input[name=hasExpress]').is(':checked')) {
            $("#logistics_form input[name=minFeeWeightExpress]")
                .rules("add",{number:true});

            $("#logistics_form input[name=minDiscountExpress]")
                .rules("add",{number:true});
            $("#logistics_form input[name=maxBoxWeightExpress]")
                .rules("add",{required:true});
            $("#logistics_form input[name=maxBoxLengthExpress]")
                .rules("add",{required:true});
            $("#logistics_form input[name=maxInsureAmountExpress]")
                .rules("add",{number:true});
            $("#logistics_form input[name=fuelAddFeeExpress]")
                .rules("add",{required:true});
            $("#logistics_form input[name=remoteAddFeeExpress]")
                .rules("add",{required:true});

            if($("#logistics_form select[name=hasVolumnFeeExpress]").val()=='Y'){
                $("#logistics_form input[name=volumnParamExpress]")
                    .rules("add",{required:true});
                $("#hasVolumnFeeExpressLabel").html("<i>*</i>体积参数：");
            }else{
                $("#logistics_form input[name=volumnParamExpress]")
                    .rules("remove","required");
                $("#hasVolumnFeeExpressLabel").html("体积参数：");
            }
        }else{
            $("#logistics_form input[name=volumnParamExpress]")
                .rules("remove","required");
            $("#logistics_form input[name=maxBoxWeightExpress]")
                .rules("remove","required");
            $("#logistics_form input[name=maxBoxLengthExpress]")
                .rules("remove","required");
            $("#logistics_form input[name=fuelAddFeeExpress]")
                .rules("remove","required");
            $("#logistics_form input[name=remoteAddFeeExpress]")
                .rules("remove","required");
        }
    }
    if(obj.attr('name') =='hasRailway'){
        if ($('input[name=hasRailway]').is(':checked')) {
            $("#logistics_form input[name=minTransportationRailway]")
                .rules("add",{required:true});
            $("#logistics_form input[name=minDiscountRailway]")
                .rules("add",{number:true});
        }else{
            $("#logistics_form input[name=minTransportationRailway]")
                .rules("remove","required");
        }
    }

}
//组装信息
function asyfun() {
    var json ={};
    json["id"]=$("#logistics_form input[name=id]").val();
    json["code"]=$("#logistics_form input[name=code]").val();
    json["name"]=$("#logistics_form input[name=name]").val();
    json["address"]=$("#logistics_form input[name=address]").val();
    json["linkman"]=$("#logistics_form input[name=linkman]").val();
    json["linkTelphone"]=$("#logistics_form input[name=linkTelphone]").val();
    json["memo"]=$("#logistics_form input[name=memo]").val();
    if ($('#logistics_form input[name=hasOcean]').is(':checked')) {
        var logisticsProviderOcean ={};
        logisticsProviderOcean["minFeeWeight"]=$("#logistics_form input[name=minFeeWeightOcean]").val();
        logisticsProviderOcean["minFeeVolumn"]=$("#logistics_form input[name=minFeeVolumnOcean]").val();
        logisticsProviderOcean["minDiscount"]=$("#logistics_form input[name=minDiscountOcean]").val();
        json["hasOcean"]='Y';
        json["logisticsProviderOcean"]=logisticsProviderOcean;
    }else{
        json["hasOcean"]='N';
    }
    if ($('input[name=hasAir]').is(':checked')) {
        var logisticsProviderAir ={};
        logisticsProviderAir["minFeeWeight"]=$("#logistics_form input[name=minFeeWeightAir]").val();
        logisticsProviderAir["hasVolumnFee"]=$("#logistics_form select[name=hasVolumnFeeAir]").val();
        logisticsProviderAir["volumnParam"]=$("#logistics_form input[name=volumnParamAir]").val();
        logisticsProviderAir["minDiscount"]=$("#logistics_form input[name=minDiscountAir]").val();
        logisticsProviderAir["hasBattery"]=$("#logistics_form select[name=hasBatteryAir]").val();
        logisticsProviderAir["maxBoxWeight"]=$("#logistics_form input[name=maxBoxWeightAir]").val();
        logisticsProviderAir["maxBoxLength"]=$("#logistics_form input[name=maxBoxLengthAir]").val();
        logisticsProviderAir["maxInsureAmount"]=$("#logistics_form input[name=maxInsureAmountAir]").val();
        json["hasAir"]='Y';
        json["logisticsProviderAir"]=logisticsProviderAir;
    }else{
        json["hasAir"]='N';
    }
    if ($('input[name=hasExpress]').is(':checked')) {
        var logisticsProviderExpress ={};
        logisticsProviderExpress["minFeeWeight"]=$("#logistics_form input[name=minFeeWeightExpress]").val();
        logisticsProviderExpress["hasVolumnFee"]=$("#logistics_form select[name=hasVolumnFeeExpress]").val();
        logisticsProviderExpress["volumnParam"]=$("#logistics_form input[name=volumnParamExpress]").val();
        logisticsProviderExpress["minDiscount"]=$("#logistics_form input[name=minDiscountExpress]").val();
        logisticsProviderExpress["hasBattery"]=$("#logistics_form select[name=hasBatteryExpress]").val();
        logisticsProviderExpress["maxBoxWeight"]=$("#logistics_form input[name=maxBoxWeightExpress]").val();
        logisticsProviderExpress["maxBoxLength"]=$("#logistics_form input[name=maxBoxLengthExpress]").val();
        logisticsProviderExpress["maxInsureAmount"]=$("#logistics_form input[name=maxInsureAmountExpress]").val();
        logisticsProviderExpress["fuelAddFee"]=$("#logistics_form input[name=fuelAddFeeExpress]").val();
        logisticsProviderExpress["remoteAddFee"]=$("#logistics_form input[name=remoteAddFeeExpress]").val();
        json["hasExpress"]='Y';
        json["logisticsProviderExpress"]=logisticsProviderExpress;
    }else{
        json["hasExpress"]='N';
    }
    if ($('#logistics_form input[name=hasRailway]').is(':checked')) {
        var logisticsProviderRailway ={};
        logisticsProviderRailway["minTransportation"]=$("#logistics_form input[name=minTransportationRailway]").val();
        logisticsProviderRailway["minDiscount"]=$("#logistics_form input[name=minDiscountRailway]").val();
        json["hasRailway"]='Y';
        json["logisticsProviderRailway"]=logisticsProviderRailway;
    }else{
        json["hasRailway"]='N';
    }

    return json;
}
//judge判断新增或更新
function judge() {
    var url =window.location.href;
    if(url.indexOf("id=")>0){
        $('#logistics_form').attr('action','api/v1/p/mms/internationalLogisticsProvider/update')
        $('span.name:first').html('物流渠道更新');
        var id = $.getUrlParam('id');
        load(id);
    }
}

//load
function load(id) {
    $.ajax({
        url:'api/v1/p/mms/internationalLogisticsProvider/detail',
        type:'post',
        data:{
            id:id
        },
        dataType:'json',
        success:function (result) {
            if (result.code == 200){
                $('#logistics_form').setForm(result.data);
                if(result.data.hasOcean =='Y'){
                    $("#logistics_form input[name=hasOcean]").attr("checked",true);
                    var myclass = $("#logistics_form input[name=hasOcean]").parent().attr('name')
                    $("#logistics_form input[name=hasOcean]").addClass('list-show');
                    $("."+myclass).show();
                    if(result.data.logisticsProviderOcean.minFeeWeight !=''){
                        $("#logistics_form input[name=biaozhunOcean]:eq(0)").attr("checked",'checked');
                    }else{
                        $("#logistics_form input[name=biaozhunOcean]:eq(1)").attr("checked",'checked');
                    }
                    $("#logistics_form input[name=minFeeWeightOcean]").val(result.data.logisticsProviderOcean.minFeeWeight);
                    $("#logistics_form input[name=minFeeVolumnOcean]").val(result.data.logisticsProviderOcean.minFeeVolumn);
                    $("#logistics_form input[name=minDiscountOcean]").val(result.data.logisticsProviderOcean.minDiscount);
                    valideServiceType( $("#logistics_form input[name=hasOcean]"))
                }
                if(result.data.hasAir =='Y'){
                    $("#logistics_form input[name=hasAir]").attr("checked",true);
                    var myclass = $("#logistics_form input[name=hasAir]").parent().attr('name')
                    $("#logistics_form input[name=hasAir]").addClass('list-show');
                    $("."+myclass).show();
                    $("#logistics_form input[name=minFeeWeightAir]").val(result.data.logisticsProviderAir.minFeeWeight);
                    $("#logistics_form select[name=hasVolumnFeeAir]").val(result.data.logisticsProviderAir.hasVolumnFee);
                    $("#logistics_form input[name=volumnParamAir]").val(result.data.logisticsProviderAir.volumnParam);
                    $("#logistics_form input[name=minDiscountAir]").val(result.data.logisticsProviderAir.minDiscount);
                    $("#logistics_form select[name=hasBatteryAir]").val(result.data.logisticsProviderAir.hasBattery);
                    $("#logistics_form input[name=maxBoxWeightAir]").val(result.data.logisticsProviderAir.maxBoxWeight);
                    $("#logistics_form input[name=maxBoxLengthAir]").val(result.data.logisticsProviderAir.maxBoxLength);
                    $("#logistics_form input[name=maxInsureAmountAir]").val(result.data.logisticsProviderAir.maxInsureAmount);
                    valideServiceType( $("#logistics_form input[name=hasAir]"))
                }
                if(result.data.hasRailway =='Y'){
                    $("#logistics_form input[name=hasRailway]").attr("checked",true);
                    var myclass = $("#logistics_form input[name=hasRailway]").parent().attr('name')
                    $("#logistics_form input[name=hasRailway]").addClass('list-show');
                    $("."+myclass).show();
                    $("#logistics_form input[name=minTransportationRailway]").val(result.data.logisticsProviderRailway.minTransportation);
                    $("#logistics_form input[name=minDiscountRailway]").val(result.data.logisticsProviderRailway.minDiscount);
                    valideServiceType( $("#logistics_form input[name=hasRailway]"))
                }
                if(result.data.hasExpress =='Y'){
                    $("#logistics_form input[name=hasExpress]").attr("checked",true);
                    var myclass = $("#logistics_form input[name=hasExpress]").parent().attr('name')
                    $("#logistics_form input[name=hasExpress]").addClass('list-show');
                    $("."+myclass).show();
                    $("#logistics_form input[name=minFeeWeightExpress]").val(result.data.logisticsProviderExpress.minFeeWeight);
                    $("#logistics_form select[name=hasVolumnFeeExpress]").val(result.data.logisticsProviderExpress.hasVolumnFee);
                    $("#logistics_form input[name=volumnParamExpress]").val(result.data.logisticsProviderExpress.volumnParam);
                    $("#logistics_form input[name=minDiscountExpress]").val(result.data.logisticsProviderExpress.minDiscount);
                    $("#logistics_form select[name=hasBatteryExpress]").val(result.data.logisticsProviderExpress.hasBattery);
                    $("#logistics_form input[name=maxBoxWeightExpress]").val(result.data.logisticsProviderExpress.maxBoxWeight);
                    $("#logistics_form input[name=maxBoxLengthExpress]").val(result.data.logisticsProviderExpress.maxBoxLength);
                    $("#logistics_form input[name=maxInsureAmountExpress]").val(result.data.logisticsProviderExpress.maxInsureAmount);
                    $("#logistics_form input[name=fuelAddFeeExpress]").val(result.data.logisticsProviderExpress.fuelAddFee);
                    $("#logistics_form input[name=remoteAddFeeExpress]").val(result.data.logisticsProviderExpress.remoteAddFee);

                    valideServiceType( $("#logistics_form input[name=hasExpress]"))
                }



            }else{
                $alert(result.message);
            }
        },
        error:function (e) {
            console.error('api/v1/p/mms/internationalLogisticsProvider/detail 接口调用失败 ' )
        }
    })
}
